#!/usr/bin/env python
# Script to load dictionary data from freedict TEI dictionaries
# David Coles (239715), 2010

TEI_NS = "{http://www.tei-c.org/ns/1.0}"

from collections import defaultdict

from xml.etree import cElementTree as ElementTree

def load(dictionary_file):
    """Takes the file name of a TEI encoded bilingual dictionary and converts 
    to a dictionary containing a set of translated words.
    """

    # Parse the TEI XML
    tree = ElementTree.ElementTree()
    tree.parse(dictionary_file)

    result = defaultdict(set)

    # Step through all the entries
    entries = tree.findall("/"+TEI_NS+"text/"+TEI_NS+"body/"+TEI_NS+"entry")
    for entry in entries:
        orth = entry.find(TEI_NS+'form/'+TEI_NS+'orth').text
        # Definitions are ';' seperated
        defs = [defn.strip() for defn in
                entry.find(TEI_NS+'sense/'+TEI_NS+'def').text.split(';')]

        # Don't add terms that look like phrases or suffixes
        if(orth.startswith("...") or orth.endswith("...") or
                orth.startswith("-")):
            continue

        # Little bit odd here since we're doing a reverse mapping
        # eg. Dictionary is French->English, we want English->French
        for defn in defs:
            # Don't add terms that look like phrases or suffixes
            if(defn.startswith("...") or defn.endswith("...") or
                    defn.startswith("-")):
                continue
            result[defn].add(orth)

    return result

if __name__ == "__main__":
    test_dict = "fra-eng.tei"
    dict = load(test_dict)

    # Print out all the keys in sorted order
    keys = sorted(dict.keys())
    for key in keys:
        print "%s: %s"%(key.encode('utf-8'),
                "; ".join(dict[key]).encode('utf-8'))
